package com.example.huhaichao.music.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.huhaichao.music.R;
import com.example.huhaichao.music.database.DBService;
import com.example.huhaichao.music.util.MusicUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhaichao on 2017/5/1.
 */

public class ViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> list;
    private List<String> olist;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(inflater);
        LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
                R.layout.music_row, null);
        TextView musicurl = (TextView) linearLayout.findViewById(R.id.musicurl);
        musicurl.setText(list.get(position));
        return linearLayout;
    }

    public ViewAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
        this.olist = list;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    public void add(String url) {
        list.add(url);
        notifyDataSetChanged();
    }

    public void remove(String url) {
        list.remove(url);
        notifyDataSetChanged();
    }

    public void SearchMusic(String music) {
        this.list = Search(music);
        MusicUtils.initMusicList3(Search(music));
        notifyDataSetChanged();
    }

    private List<String> Search(String music) {
        //如果查询的值不是空的就走进来然后返回搜索后的值，否则返回原本的值
        if (music != null && music.length() > 0) {
            //new一个新的容器
            ArrayList<String> area = new ArrayList<String>();
            //循环olist集合
            for (String a : this.olist) {
                //判断a里面如果包含了搜索的值，有就添加，没有否则就不添加
                if (a.toString().indexOf(music) != -1) {
                    area.add(a);
                }
            }
            return area;
        } else {
            return this.olist;
        }
    }
}
